package com.hyh.cart.service.impl;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.cart.dao.CartMapper;
import com.hyh.cart.domain.Cart;
import com.hyh.cart.result.PageResult;
import com.hyh.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 添加商品信息到购物车
    @Override
    public PageResult addCart(Cart cart) {
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getProductId,cart.getProductId());
        qw.eq(Cart::getUserId,cart.getUserId());
        qw.eq(Cart::getAddressId,cart.getAddressId());
        Cart cart1 = cartMapper.selectOne(qw);
        cart.setPriceTotal(cart.getPrice() * cart.getCount());
        if(Objects.nonNull(cart1)){
            cart1.setCount(cart1.getCount() + cart.getCount());
            cart1.setPriceTotal(cart1.getPrice() * cart1.getCount());
            return cartMapper.updateById(cart1) > 0 ?
                    new PageResult(HttpStatus.HTTP_OK,null,"添加成功") :
                    new PageResult(HttpStatus.HTTP_CREATED,null,"添加失败");
        }
        return cartMapper.insert(cart) > 0 ?
                new  PageResult(HttpStatus.HTTP_OK,null,"添加成功") :
                new PageResult(HttpStatus.HTTP_CREATED,null,"添加失败");
    }

    // 商品数量加一
    @Override
    public PageResult addOne(Long id) {
        Cart cart = cartMapper.selectById(id);
        cart.setCount(cart.getCount() + 1);
        cartMapper.updateById(cart);
        return new PageResult(HttpStatus.HTTP_OK,null,"添加成功");
    }

    // 商品数量减一
    @Override
    public PageResult subOne(Long id) {
        Cart cart = cartMapper.selectById(id);
        if(cart.getCount() > 2)
            cart.setCount(cart.getCount() + - 1 );
        cartMapper.updateById(cart);
        return new PageResult(HttpStatus.HTTP_OK,null,"删除成功");
    }

    // 计算总价
    @Override
    public PageResult totalPrice(Long id) {
        Cart cart = cartMapper.selectById(id);
        cart.setPriceTotal(cart.getPrice() * cart.getCount());
        cartMapper.updateById(cart);
        return new PageResult(HttpStatus.HTTP_OK,cart,"计算成功");
    }

    // 查询购物车列表
    @Override
    public PageResult list(Long userId) {
        return new PageResult(HttpStatus.HTTP_OK,
                cartMapper.selectList(new LambdaQueryWrapper<Cart>()
                        .eq(Cart::getUserId,userId)),"查询成功");
    }


    // 清空购物车
    @Override
    public PageResult delete(Long userId) {
        int delete = cartMapper.delete(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserId, userId));
        if(delete > 0){
            return new PageResult(HttpStatus.HTTP_OK,null,"清空成功");
        }
        return new PageResult(HttpStatus.HTTP_INTERNAL_ERROR,null,"清空失败");
    }

    // 删除购物车中的某一项
    @Override
    public PageResult deleteOne(Long id) {
        int flag = cartMapper.deleteById(id);
        if(flag > 0){
            return new PageResult(HttpStatus.HTTP_OK,null,"删除成功");
        }
        return new PageResult(HttpStatus.HTTP_INTERNAL_ERROR,null,"删除失败");
    }

    // 查询购物车某个商品
    @Override
    public Cart getCart(Long id) {
        return cartMapper.selectById(id);
    }
}
